package com.whlilong.jxc.view;

import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;

import org.eclipse.jface.dialogs.IInputValidator;
import org.eclipse.jface.dialogs.InputDialog;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.viewers.TableViewer;
import org.eclipse.jface.window.Window;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.KeyEvent;
import org.eclipse.swt.events.KeyListener;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.events.MouseListener;
import org.eclipse.swt.events.TraverseEvent;
import org.eclipse.swt.events.TraverseListener;
import org.eclipse.swt.graphics.Font;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.layout.FormAttachment;
import org.eclipse.swt.layout.FormData;
import org.eclipse.swt.layout.FormLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.swt.widgets.Text;
import org.eclipse.ui.part.ViewPart;

import com.swtdesigner.SWTResourceManager;
import com.whlilong.jxc.db.DbManager;
import com.whlilong.jxc.dialog.PayDialog;
import com.whlilong.jxc.model.Goodsinfo;
import com.whlilong.jxc.model.Goodsreceiving;
import com.whlilong.jxc.model.Iostorage;
import com.whlilong.jxc.model.Selldetail;
import com.whlilong.jxc.model.Sellmain;
import com.whlilong.jxc.model.Storage;
import com.whlilong.jxc.sys.ExtentionInfo;
import com.whlilong.jxc.sys.SaveLogInfo;
import com.whlilong.jxc.sys.SetTableColColorListener;
import com.whlilong.jxc.ui.provider.AcceleratorContentProvider;
import com.whlilong.jxc.ui.provider.AcceleratorLableProvider;
import com.whlilong.jxc.ui.provider.ClientGoodsInfoLableProvider;
import com.whlilong.jxc.ui.provider.GoodsInfoContentProvider;

/**
 * 武汉立隆商业管理系统--前台管理 视图
 * 
 * @author sjy
 * 
 */

public class ClientManagerView extends ViewPart {

	private Label title;
	private Label title2;
	private Group group;
	private Label splabel;
	private Text sptext;
	private Label tslabel;
	private Label tsinfo;
	private Label time;
	private Label xsdh;
	private String curMonths;
	private String curDays;
	private Label dh;
	private Label syylabel;
	private Label syy;
	private Label sdhj;
	private Label je;
	private Label jk;
	private Label jkbh;
	private Label zklabel;
	private Label zk;
	private Label sdsslabel;
	private Label sdss;
	private Label modelabel;
	private Label mode;
	private Label sllabel;
	private Label sl;
	private Label sdzllabel;
	private Label sdzl;
	private TableViewer tableViewer1;
	private TableViewer tableViewer;
	private String dataTimeFormat;
	private Table table;

	/**
	 * 前台销售明细显示表
	 */
	protected List<Selldetail> detailList = new ArrayList<Selldetail>();
	protected boolean isAdd = true;
	private TableViewer payViewer;
	private Table paytable;
	/**
	 * 商品销售信息主表
	 */
	private List<Sellmain> sellmainList = new ArrayList<Sellmain>();
	/**
	 * 商品销售信息明细表
	 */
	private List<Selldetail> selldetailList = new ArrayList<Selldetail>();
	private Button exit;
	private Composite composite;
	private Shell shell = null;
	/**
	 * 付款金额
	 */
	private double payMoney = 0;
	/**
	 * 合计金额
	 */
	private double sumMoney = 0;
	/**
	 * 找零金额
	 */
	private double changeMoney = 0;

	private static final SimpleDateFormat df = new SimpleDateFormat("yyyyMMdd");
	private static final SimpleDateFormat df2 = new SimpleDateFormat(
			"yyyy-MM-dd HH:mm:ss");
	private static final String date = df.format(new Date());
	private static final String datetime = df2.format(new Date());
	private static final NumberFormat f = new DecimalFormat("#0.00");

	public ClientManagerView() {
		// TODO Auto-generated constructor stub
	}

	@Override
	public void createPartControl(Composite parent) {
		parent.setForeground(SWTResourceManager.getColor(0, 0, 0));
		// TODO Auto-generated method stub

		Rectangle screenSize = Display.getDefault().getClientArea();
		parent.setBackground(Display.getCurrent().getSystemColor(
				SWT.COLOR_BLACK));
		composite = new Composite(parent, SWT.FILL_WINDING);
		composite.setBackground(parent.getBackground());
		FormLayout layout = new FormLayout();
		layout.marginTop = 5;
		layout.marginLeft = 5;
		layout.marginRight = 5;
		layout.marginBottom = 10;
		composite.setLayout(layout);
		composite.setFont(parent.getFont());

		title = new Label(composite, SWT.CENTER);
		title.setBackground(parent.getBackground());
		title.setFont(new Font(Display.getCurrent(), "隶书", 30, SWT.NONE));
		title.setForeground(Display.getCurrent()
				.getSystemColor(SWT.COLOR_WHITE));
		title.setText("武汉立隆商务信息管理平台");
		FormData fd = new FormData();
		fd.width = screenSize.width;
		fd.height = 40;
		fd.right = new FormAttachment(100, 0);
		fd.left = new FormAttachment(0, 0);
		title.setLayoutData(fd);

		title2 = new Label(composite, SWT.CENTER);
		title2.setBackground(parent.getBackground());
		title2.setFont(new Font(Display.getCurrent(), "宋体", 12, SWT.NONE));
		title2.setForeground(Display.getCurrent().getSystemColor(
				SWT.COLOR_WHITE));
		title2.setText("武汉市立隆电子科技工程有限公司   电话：027-85506209");
		fd = new FormData();
		fd.height = 40;
		fd.top = new FormAttachment(title, 10);
		fd.right = new FormAttachment(100, -10);
		fd.left = new FormAttachment(0, 0);
		title2.setLayoutData(fd);

		createTableView(composite);

		createAccelerator(composite);

		createGroupText(composite);
		
		initial();

		addListener();
	}

	private void addListener() {
		composite.addTraverseListener(new TraverseListener() {

			@Override
			public void keyTraversed(TraverseEvent e) {
				// 如果按的是Esc键就不执行操作
				if (e.detail == SWT.TRAVERSE_ESCAPE) {

					if (MessageDialog.openConfirm(null, "提示", "确定要退出前台收银系统吗?")) {
						e.doit = true;
						Display.getCurrent().getActiveShell().close();
					} else
						e.doit = false;
				}
			}
		});

		sptext.addKeyListener(new KeyListener() {
			@Override
			public void keyPressed(KeyEvent e) {
				int c = 0;// 合计数量
				double discout = 0.0;// 合计折扣金额
				double sum = 0.0;// 合计
				if (e.character == SWT.CR) {
					isAdd = true;// 对应goodsid的商品是否重新添加到detailList中
					tsinfo.setText("");
					String goodsid = sptext.getText();
					if (goodsid.equals("") || goodsid == null) {
						if (detailList.size() > 0) {
							/**
							 * 执行商品销售动作
							 */
							sellGoods();
						} else {
							tsinfo.setText("没有要付款商品！");
						}
					} else {
						int[] idBuffer = { 0 };
						Object[] idObject = { goodsid };
						List<Goodsinfo> gsinfo = (List) DbManager
								.getInstance()
								.search(
										"com.whlilong.jxc.editor.getGoodsInfo.getGoodsid",
										idBuffer, idObject);// 获得商品档案信息
						List<Storage> storageinfo = (List) DbManager
								.getInstance()
								.search(
										"com.whlilong.jxc.editor.getStorage.getgoodsid",
										idBuffer, idObject);// 获得库存信息
						if (storageinfo.size() > 0) {
							if (storageinfo.get(0).getStoragecount() <= 0)
								tsinfo.setText("该商品库存不足。。。");
						} else {
							if (gsinfo.size() > 0)
								tsinfo.setText("该商品还未入库。。。");
							else {
								tsinfo.setText("该商品不存在。。。");
								sptext.setText("");
								return;
							}
						}

						if (detailList.size() > 0) {
							for (int i = 0; i < detailList.size(); i++) {
								if (detailList.get(i).getGoodsid().equals(
										gsinfo.get(0).getGoodsid())) {
									int count = detailList.get(i).getCout() + 1;
									double price = detailList.get(i)
											.getSellprice().doubleValue();
									double retailprice = detailList.get(i)
											.getRetailprice().doubleValue();
									double dscout = (price - retailprice)
											* count;
									double money = count * price;
									table.getItem(i).setText(3,
											String.valueOf(count));
									table.getItem(i).setText(4,
											String.valueOf(f.format(dscout)));
									table.getItem(i).setText(6,
											String.valueOf(money));
									detailList.get(i).setCout(count);
									detailList.get(i).setDiscountmoney(
											BigDecimal.valueOf(dscout));
									detailList.get(i).setMoney(
											BigDecimal.valueOf(money));

									isAdd = false;
									tsinfo.setText("");
								}

							}
						}
						if (detailList.size() <= 0 || isAdd) {
							Selldetail selldetail = new Selldetail();
							if (gsinfo.size() > 0 && storageinfo.size() > 0) {
								selldetail.setGoodsid(gsinfo.get(0)
										.getGoodsid());
								selldetail.setBarcode(gsinfo.get(0)
										.getGoodshh());
								selldetail.setBase(storageinfo.get(0)
										.getBaseid());
								selldetail.setCout(1);
								selldetail.setDiscountmode("0");// 0--正常销售
								// 1--改价销售
								// 2--会员价销售
								selldetail.setGoodsname(gsinfo.get(0)
										.getGoodsname());
								selldetail.setSellprice(gsinfo.get(0).getLsj());
								double money = gsinfo.get(0).getLsj()
										.doubleValue();
								selldetail.setMoney(BigDecimal.valueOf(money));
								selldetail.setRetailprice(gsinfo.get(0)
										.getLsj());
								selldetail.setSellid(dh.getText());
								selldetail.setSelldate(new Date());
								selldetail.setSellmode("0");// 0--前台销售
								selldetail.setStaff(SaveLogInfo.getInstance()
										.getUsername());
								selldetail.setDiscountmoney(BigDecimal
										.valueOf(0));
								detailList.add(selldetail);
								tsinfo.setText("");

							} else {
								tsinfo.setText("该商品还未入库。。。");
							}

						}
						if (isAdd)
							tableViewer.setInput(detailList);
						table.setSelection(table.getItemCount() - 1);
						sptext.setText("");
					}
				}
				if (e.keyCode == SWT.ARROW_RIGHT) {
					String sprice = sptext.getText();
					if (!sprice.equals("") || sprice != null) {
						String regex = "[1-9][0-9]*.*[0-9]+?";
						if (sprice.matches(regex)) {
							if (table.getItemCount() > 0) {
								int row = table.getSelectionIndex();
								int cout = detailList.get(row).getCout();
								double money = cout
										* Double.parseDouble(sprice);
								double discount = (Double.parseDouble(sprice) - detailList
										.get(row).getRetailprice()
										.doubleValue())
										* cout;
								table.getItem(row).setText(4,
										f.format(discount));
								table.getItem(row).setText(5, sprice);
								table.getItem(row).setText(6,
										String.valueOf(money));
								detailList.get(row).setSellprice(
										BigDecimal.valueOf(Double
												.parseDouble(sprice)));
								detailList.get(row).setMoney(
										BigDecimal.valueOf(money));
								detailList.get(row).setDiscountmoney(
										BigDecimal.valueOf(discount));
							}
							tsinfo.setText("");
							sptext.setText("");
						} else
							tsinfo.setText("改价的格式不对!");
						sptext.setText("");
					}
				}
				if (e.keyCode == SWT.ARROW_LEFT) {
					String scout = sptext.getText();
					if (!scout.equals("") || scout != null) {
						String regex = "[0-9]+";
						if (scout.matches(regex)) {
							if (table.getItemCount() > 0) {
								int row = table.getSelectionIndex();
								double price = detailList.get(row).getSellprice().doubleValue();
								double money = price
										* Integer.parseInt(scout);
								double discount = (price - detailList
										.get(row).getRetailprice()
										.doubleValue())
										* Integer.parseInt(scout);
								table.getItem(row).setText(4,
										f.format(discount));
								table.getItem(row).setText(6,
										String.valueOf(money));
								table.getItem(row).setText(3, scout);
								detailList.get(row).setCout(
										Integer
												.parseInt(scout));
								detailList.get(row).setMoney(
										BigDecimal.valueOf(money));
								detailList.get(row).setDiscountmoney(
										BigDecimal.valueOf(discount));
							}
							tsinfo.setText("");
							sptext.setText("");
						} else
							tsinfo.setText("更改数量的格式不对!");
						sptext.setText("");
					}
				}

				if (detailList.size() > 0) {
					for (int i = 0; i < detailList.size(); i++) {
						c += detailList.get(i).getCout();
						discout += detailList.get(i).getDiscountmoney()
								.doubleValue();
						sum += detailList.get(i).getMoney().doubleValue();
					}
					sl.setText(String.valueOf(c));// 数量
					je.setText(BigDecimal.valueOf(sum).toString());// 上单合计
					zk.setText(String.valueOf(f.format(discout)));// 折扣金额
					sdss.setText("0.00");
					sdzl.setText("0.00");
				}
			}

			@Override
			public void keyReleased(KeyEvent e) {
				// TODO Auto-generated method stub

			}
		});

		exit.addMouseListener(new MouseListener() {

			@Override
			public void mouseDoubleClick(MouseEvent e) {
				// TODO Auto-generated method stub

			}

			@Override
			public void mouseDown(MouseEvent e) {
				// TODO Auto-generated method stub
				if (MessageDialog.openConfirm(null, "提示", "确定要退出前台收银系统吗?"))
					Display.getCurrent().getActiveShell().close();
			}

			@Override
			public void mouseUp(MouseEvent e) {
				// TODO Auto-generated method stub

			}

		});
	}

	protected void sellGoods() {
		// TODO 商品销售
		Shell subShell = new Shell(shell, SWT.DIALOG_TRIM);
		PayDialog pd = new PayDialog(subShell, je.getText());

		// /**
		// * 付款金额格式校对
		// */
		// IInputValidator validator = new IInputValidator() {
		//
		// @Override
		// public String isValid(String newText) {
		// // TODO Auto-generated method stub
		// String regex = "[0-9]+.*[0-9]+?";
		// if (!newText.matches(regex))
		// return "付款金额格式有误！";
		// else {
		// payMoney = Double.parseDouble(newText);
		// sumMoney = Double.parseDouble(je.getText());
		// changeMoney = payMoney - sumMoney;
		// if (changeMoney < 0)
		// return "付款金额不足！";
		// else
		// return null;
		// }
		// }
		//
		// };
		// InputDialog payDialog = new InputDialog(subShell, "商品付款", "付款金额：",
		// "0.00", validator);
		if (pd.open() == Window.OK) {
			if (pd.isSuccess()) {
				payMoney = pd.getPay();
				changeMoney = pd.getChange();
				sdss.setText(String.valueOf(payMoney));
				sdzl.setText(String.valueOf(f.format(changeMoney)));

				
				writeData(detailList);
				writeIostorage(detailList);
				modifyStorage(detailList);

				detailList.clear();
				tableViewer.setInput(detailList);
				
				initial();

			}
		}

	}

	/**
	 * 将销售信息写入库存出入库信息明细表
	 */
	private void writeIostorage(List<Selldetail> dlist) {
		// TODO 写出入库信息明细表（Iostorage）
		if (dlist != null && dlist.size() > 0) {
			for (int i = 0; i < dlist.size(); i++) {
				Selldetail detail = (Selldetail) dlist.get(i);
				Iostorage iodetail = new Iostorage();
				iodetail.setBarcode(detail.getBarcode());
				iodetail.setCout(detail.getCout());
				iodetail.setGoodsid(detail.getGoodsid());
				iodetail.setGoodsname(detail.getGoodsname());
				iodetail.setIodate(new Date());
				iodetail.setIostatus("0");
				iodetail.setRetailmoney(detail.getMoney());
				double rprice = detail.getRetailprice().doubleValue();
				int c = detail.getCout();
				double storagemoney = rprice * c ;
				iodetail.setStoragemoney(BigDecimal.valueOf(storagemoney));
				iodetail.setUnit("");
				
				DbManager.getInstance().insert(iodetail);
			}
		}
		
	}

	private void modifyStorage(List<Selldetail> detailst) {
		// TODO 修改库存数量
		
		for(int i = 0;i<detailst.size();i++){
			Selldetail dtl = detailst.get(i);
			int[] idBuffer = { 0 };
			Object[] idObject = { dtl.getGoodsid() };
			
			List<Storage> storageinfo = (List) DbManager
					.getInstance()
					.search(
							"com.whlilong.jxc.editor.getStorage.getgoodsid",
							idBuffer, idObject);// 获得库存信息
			int storageCout = storageinfo.get(0).getStoragecount();
			int cout = dtl.getCout();
			storageinfo.get(0).setStoragecount(storageCout-cout);
			double storagemoney = (storageCout-cout)*detailst.get(i).getRetailprice().doubleValue();
			storageinfo.get(0).setStoragemoney(BigDecimal.valueOf(storagemoney));
			DbManager.getInstance().update(storageinfo.get(0));
			
		}
	}

	private void initial() {
		// TODO 初始化界面
		String sellid = "";
		int id = 1;
		long model = Long.parseLong(date) * 100000;
		sellmainList = ExtentionInfo.getInstance().getSellmain();
		if (sellmainList.size() > 0)
			id = sellmainList.get(sellmainList.size() - 1).getId() + 1;
		model += id;
		sellid = String.valueOf(model);
		dh.setText("XS" + sellid);
		
		syy.setText(SaveLogInfo.getInstance().getUsername());
		
		jkbh.setText("H200911041001");
		
	}

	private void writeData(List<Selldetail> dtlList) {
		// TODO 将销售数据写入数据库
		/**
		 * 保存销售主表
		 */
		Sellmain sm = new Sellmain();
		sm.setBase(dtlList.get(0).getBase());
		sm.setChange(BigDecimal.valueOf(Double.valueOf(sdzl.getText())));
		sm.setCout(Integer.parseInt(sl.getText()));
		sm.setDiscountmoney(BigDecimal.valueOf(Double.valueOf(zk.getText())));
		sm.setPaymoney(BigDecimal.valueOf(payMoney));
		sm.setSelldate((new Date()));
		sm.setStaff(SaveLogInfo.getInstance().getUsername());
		sm.setSummoney(BigDecimal.valueOf(Double.valueOf(je.getText())));
		sm.setSellid(dh.getText());
		DbManager.getInstance().insert(sm);
		
		/**
		 * 保存销售明细表
		 */
		for(int i =0;i<dtlList.size();i++){
			Selldetail sd = dtlList.get(i);
			DbManager.getInstance().insert(sd);
		}
		
		
		
	}

	private void createGroupText(Composite parent) {
		// TODO Auto-generated method stub
		group = new Group(parent, SWT.NONE);
		group.setBackground(SWTResourceManager.getColor(0, 0, 0));
		group.setFont(SWTResourceManager.getFont("宋体", 16, SWT.NORMAL));
		FormData fd = new FormData();
		fd.left = new FormAttachment(2, 0);
		fd.top = new FormAttachment(table, 5);
		fd.bottom = new FormAttachment(100, 0);
		group.setLayoutData(fd);

		FormLayout layout = new FormLayout();
		layout.marginTop = 5;
		layout.marginLeft = 5;
		layout.marginRight = 5;
		group.setLayout(layout);

		payViewer = new TableViewer(parent, SWT.BORDER);
		payViewer.setUseHashlookup(true);
		paytable = payViewer.getTable();
		paytable.setBackground(parent.getBackground());
		fd = new FormData();
		fd.top = new FormAttachment(table, 15);
		fd.left = new FormAttachment(group, 5);
		fd.right = new FormAttachment(100);
		fd.bottom = new FormAttachment(100, 0);
		paytable.setLayoutData(fd);

		paytable.setHeaderVisible(false);
		paytable.setLinesVisible(false);

		paytable.addListener(SWT.MeasureItem, new Listener() {
			public void handleEvent(Event event) {
				event.width = paytable.getGridLineWidth(); // 设置宽度

				event.height = (int) Math.floor(event.gc.getFontMetrics()
						.getHeight() * 1.5); // 设置高度为字体高度的5倍
				paytable.setForeground(Display.getCurrent().getSystemColor(
						SWT.COLOR_WHITE));
			}
		});

		final TableColumn payId = new TableColumn(paytable, SWT.NONE);
		payId.setText("币种");
		payId.setWidth(50);
		final TableColumn money = new TableColumn(paytable, SWT.CENTER);
		money.setText("金额");
		money.setWidth(100);
		final TableColumn pay = new TableColumn(paytable, SWT.CENTER);
		pay.setText("付款方式");
		pay.setWidth(100);

		payViewer.setContentProvider(new AcceleratorContentProvider());
		payViewer.setLabelProvider(new AcceleratorLableProvider());
		payViewer.setInput(getAccelerator());

		splabel = new Label(group, SWT.NONE);
		splabel.setBackground(group.getBackground());
		splabel.setFont(new Font(Display.getCurrent(), "宋体", 20, SWT.NONE));
		splabel.setForeground(Display.getCurrent().getSystemColor(
				SWT.COLOR_WHITE));
		fd = new FormData();
		fd.height = 40;
		fd.left = new FormAttachment(0, 0);
		splabel.setLayoutData(fd);
		splabel.setText("商品:");

		sptext = new Text(group, SWT.NONE);
		sptext.setFont(new Font(Display.getCurrent(), "宋体", 20, SWT.NONE));
		fd = new FormData();
		fd.height = 40;
		fd.width = 400;
		fd.left = new FormAttachment(splabel, 10);
		sptext.setLayoutData(fd);
		sptext.selectAll();
		sptext.setFocus();

		tslabel = new Label(group, SWT.NONE);
		tslabel.setBackground(group.getBackground());
		tslabel.setFont(new Font(Display.getCurrent(), "隶书", 16, SWT.NONE));
		tslabel.setForeground(Display.getCurrent().getSystemColor(
				SWT.COLOR_WHITE));
		fd = new FormData();
		fd.height = 40;
		fd.top = new FormAttachment(table, 8);
		fd.left = new FormAttachment(sptext, 10);
		tslabel.setLayoutData(fd);
		tslabel.setText("友情提示:");

		tsinfo = new Label(group, SWT.NONE);
		tsinfo.setBackground(group.getBackground());
		tsinfo.setFont(new Font(Display.getCurrent(), "宋体", 12, SWT.NONE));
		tsinfo.setForeground(Display.getCurrent().getSystemColor(
				SWT.COLOR_GREEN));
		fd = new FormData();
		fd.height = 40;
		fd.width = 200;
		fd.top = new FormAttachment(table, 10);
		fd.left = new FormAttachment(tslabel, 10);
		tsinfo.setLayoutData(fd);
		tsinfo.setText("前台收银");

		dataTimeFormat = new SimpleDateFormat("yyyy-MM-dd hh:MM:ss")
				.format(Calendar.getInstance().getTime());

		time = new Label(group, SWT.NONE);
		time.setBackground(group.getBackground());
		time.setFont(new Font(Display.getCurrent(), "宋体", 12, SWT.NONE));
		time
				.setForeground(Display.getCurrent().getSystemColor(
						SWT.COLOR_WHITE));
		fd = new FormData();
		fd.height = 40;
		fd.width = 200;
		fd.left = new FormAttachment(tsinfo, 10);
		fd.right = new FormAttachment(100, 0);
		time.setLayoutData(fd);
		updateTime(time);// 实时更新时间。。。

		xsdh = new Label(group, SWT.NONE);
		xsdh.setBackground(group.getBackground());
		xsdh.setFont(new Font(Display.getCurrent(), "宋体", 12, SWT.NONE));
		xsdh
				.setForeground(Display.getCurrent().getSystemColor(
						SWT.COLOR_WHITE));
		fd = new FormData();
		fd.height = 30;
		fd.top = new FormAttachment(splabel, 10);
		xsdh.setLayoutData(fd);
		xsdh.setText("销售单号:");

		dh = new Label(group, SWT.NONE);
		dh.setBackground(group.getBackground());
		dh.setFont(new Font(Display.getCurrent(), "宋体", 12, SWT.NONE));
		dh.setForeground(Display.getCurrent().getSystemColor(SWT.COLOR_WHITE));
		fd = new FormData();
		fd.height = 30;
		fd.top = new FormAttachment(splabel, 10);
		fd.left = new FormAttachment(xsdh, 10);
		dh.setLayoutData(fd);
		

		syylabel = new Label(group, SWT.NONE);
		syylabel.setBackground(group.getBackground());
		syylabel.setFont(new Font(Display.getCurrent(), "宋体", 12, SWT.NONE));
		syylabel.setForeground(Display.getCurrent().getSystemColor(
				SWT.COLOR_WHITE));
		fd = new FormData();
		fd.height = 30;
		fd.top = new FormAttachment(splabel, 10);
		fd.left = new FormAttachment(dh, 40);
		syylabel.setLayoutData(fd);
		syylabel.setText("收银员:");

		syy = new Label(group, SWT.NONE);
		syy.setBackground(group.getBackground());
		syy.setFont(new Font(Display.getCurrent(), "宋体", 12, SWT.NONE));
		syy.setForeground(Display.getCurrent().getSystemColor(SWT.COLOR_WHITE));
		fd = new FormData();
		fd.height = 30;
		fd.top = new FormAttachment(splabel, 10);
		fd.left = new FormAttachment(syylabel, 10);
		syy.setLayoutData(fd);
		

		sdhj = new Label(group, SWT.NONE);
		sdhj.setBackground(group.getBackground());
		sdhj.setFont(new Font(Display.getCurrent(), "宋体", 12, SWT.NONE));
		sdhj
				.setForeground(Display.getCurrent().getSystemColor(
						SWT.COLOR_WHITE));
		fd = new FormData();
		fd.height = 30;
		fd.top = new FormAttachment(splabel, 10);
		fd.left = new FormAttachment(syy, 40);
		sdhj.setLayoutData(fd);
		sdhj.setText("上单合计:");

		je = new Label(group, SWT.NONE);
		je.setBackground(group.getBackground());
		je.setFont(new Font(Display.getCurrent(), "隶书", 25, SWT.NONE));
		je.setForeground(Display.getCurrent().getSystemColor(SWT.COLOR_RED));
		fd = new FormData();
		fd.height = 30;
		fd.width = 200;
		fd.top = new FormAttachment(splabel, 5);
		fd.left = new FormAttachment(sdhj, 5);
		je.setLayoutData(fd);
		je.setText("0.00");

		jk = new Label(group, SWT.NONE);
		jk.setBackground(group.getBackground());
		jk.setFont(new Font(Display.getCurrent(), "宋体", 12, SWT.NONE));
		jk.setForeground(Display.getCurrent().getSystemColor(SWT.COLOR_WHITE));
		fd = new FormData();
		fd.height = 30;
		fd.top = new FormAttachment(xsdh, 10);
		jk.setLayoutData(fd);
		jk.setText("金卡编号:");

		jkbh = new Label(group, SWT.NONE);
		jkbh.setBackground(group.getBackground());
		jkbh.setFont(new Font(Display.getCurrent(), "宋体", 12, SWT.NONE));
		jkbh
				.setForeground(Display.getCurrent().getSystemColor(
						SWT.COLOR_WHITE));
		fd = new FormData();
		fd.height = 30;
		fd.top = new FormAttachment(xsdh, 10);
		fd.left = new FormAttachment(jk, 10);
		jkbh.setLayoutData(fd);
		

		zklabel = new Label(group, SWT.NONE);
		zklabel.setBackground(group.getBackground());
		zklabel.setFont(new Font(Display.getCurrent(), "宋体", 12, SWT.NONE));
		zklabel.setForeground(Display.getCurrent().getSystemColor(
				SWT.COLOR_WHITE));
		fd = new FormData();
		fd.height = 30;
		fd.top = new FormAttachment(syylabel, 10);
		fd.left = new FormAttachment(jkbh, 55);
		zklabel.setLayoutData(fd);
		zklabel.setText("折  扣:");

		zk = new Label(group, SWT.NONE);
		zk.setBackground(group.getBackground());
		zk.setFont(new Font(Display.getCurrent(), "宋体", 12, SWT.NONE));
		zk.setForeground(Display.getCurrent().getSystemColor(SWT.COLOR_WHITE));
		fd = new FormData();
		fd.height = 30;
		fd.width = 150;
		fd.top = new FormAttachment(xsdh, 10);
		fd.left = new FormAttachment(zklabel, 10);
		zk.setLayoutData(fd);
		zk.setText("0.0");

		sdsslabel = new Label(group, SWT.NONE);
		sdsslabel.setBackground(group.getBackground());
		sdsslabel.setFont(new Font(Display.getCurrent(), "宋体", 12, SWT.NONE));
		sdsslabel.setForeground(Display.getCurrent().getSystemColor(
				SWT.COLOR_WHITE));
		fd = new FormData();
		fd.height = 30;
		fd.top = new FormAttachment(xsdh, 10);
		fd.left = new FormAttachment(zk, 25);
		sdsslabel.setLayoutData(fd);
		sdsslabel.setText("上单实收:");

		sdss = new Label(group, SWT.NONE);
		sdss.setBackground(group.getBackground());
		sdss.setFont(new Font(Display.getCurrent(), "隶书", 25, SWT.NONE));
		sdss.setForeground(Display.getCurrent().getSystemColor(SWT.COLOR_RED));
		fd = new FormData();
		fd.height = 30;
		fd.width = 100;
		fd.top = new FormAttachment(xsdh, 5);
		fd.left = new FormAttachment(sdsslabel, 5);
		sdss.setLayoutData(fd);
		sdss.setText("0.00");

		modelabel = new Label(group, SWT.NONE);
		modelabel.setBackground(group.getBackground());
		modelabel.setFont(new Font(Display.getCurrent(), "宋体", 12, SWT.NONE));
		modelabel.setForeground(Display.getCurrent().getSystemColor(
				SWT.COLOR_WHITE));
		fd = new FormData();
		fd.height = 30;
		fd.top = new FormAttachment(jk, 10);
		modelabel.setLayoutData(fd);
		modelabel.setText("模    式:");

		mode = new Label(group, SWT.NONE);
		mode.setBackground(group.getBackground());
		mode.setFont(new Font(Display.getCurrent(), "宋体", 12, SWT.NONE));
		mode
				.setForeground(Display.getCurrent().getSystemColor(
						SWT.COLOR_WHITE));
		fd = new FormData();
		fd.height = 30;
		fd.top = new FormAttachment(jk, 10);
		fd.left = new FormAttachment(modelabel, 10);
		mode.setLayoutData(fd);
		mode.setText("销售");

		sllabel = new Label(group, SWT.NONE);
		sllabel.setBackground(group.getBackground());
		sllabel.setFont(new Font(Display.getCurrent(), "宋体", 12, SWT.NONE));
		sllabel.setForeground(Display.getCurrent().getSystemColor(
				SWT.COLOR_WHITE));
		fd = new FormData();
		fd.height = 30;
		fd.top = new FormAttachment(jk, 10);
		fd.left = new FormAttachment(mode, 130);
		sllabel.setLayoutData(fd);
		sllabel.setText("数  量:");

		sl = new Label(group, SWT.NONE);
		sl.setBackground(group.getBackground());
		sl.setFont(new Font(Display.getCurrent(), "宋体", 12, SWT.NONE));
		sl.setForeground(Display.getCurrent().getSystemColor(SWT.COLOR_WHITE));
		fd = new FormData();
		fd.height = 30;
		fd.top = new FormAttachment(jk, 10);
		fd.left = new FormAttachment(sllabel, 10);
		sl.setLayoutData(fd);
		sl.setText("0");

		sdzllabel = new Label(group, SWT.NONE);
		sdzllabel.setBackground(group.getBackground());
		sdzllabel.setFont(new Font(Display.getCurrent(), "宋体", 12, SWT.NONE));
		sdzllabel.setForeground(Display.getCurrent().getSystemColor(
				SWT.COLOR_WHITE));
		fd = new FormData();
		fd.height = 30;
		fd.top = new FormAttachment(jk, 10);
		fd.left = new FormAttachment(sl, 165);
		sdzllabel.setLayoutData(fd);
		sdzllabel.setText("上单找零:");

		sdzl = new Label(group, SWT.NONE);
		sdzl.setBackground(group.getBackground());
		sdzl.setFont(new Font(Display.getCurrent(), "隶书", 25, SWT.NONE));
		sdzl.setForeground(Display.getCurrent().getSystemColor(SWT.COLOR_RED));
		fd = new FormData();
		fd.height = 30;
		fd.width = 100;
		fd.top = new FormAttachment(jk, 5);
		fd.left = new FormAttachment(sdzllabel, 5);
		sdzl.setLayoutData(fd);
		sdzl.setText("0.00");

		exit = new Button(group, SWT.NONE);
		fd = new FormData();
		fd.top = new FormAttachment(jk, 10);
		fd.left = new FormAttachment(sdzl, 40);
		exit.setLayoutData(fd);
		exit.setText("退出系统");

	}

	private void updateTime(final Label tm) {
		// 时间定时器一秒钟更新一次
		Timer timer = new Timer();

		try {
			timer.schedule(new TimerTask() {
				public void run() {

					Display.getDefault().asyncExec(new Runnable() {
						public void run() {
							dataTimeFormat = new SimpleDateFormat(
									"yyyy-MM-dd HH:mm:ss").format(Calendar
									.getInstance().getTime());
							if (!tm.isDisposed())// 判断控件是否被销毁，可以去除wideget is
								// disposed 异常。。。
								tm.setText(dataTimeFormat);
						}
					});

				}
			}, 0, 1 * 1000);
		} catch (Exception e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
	}

	private void createAccelerator(Composite parent) {
		// TODO Auto-generated method stub

		tableViewer1 = new TableViewer(parent, SWT.BORDER);
		tableViewer1.setUseHashlookup(true);
		final Table table1 = tableViewer1.getTable();
		table1.setBackground(parent.getBackground());
		FormData fd = new FormData();
		fd.height = 400;
		fd.top = new FormAttachment(title2, 10);
		fd.left = new FormAttachment(table, 5);
		fd.right = new FormAttachment(100);
		table1.setLayoutData(fd);
		/*
		 * GridData gd = new GridData(GridData.FILL_BOTH); gd.verticalIndent =
		 * 20; table.setLayoutData(gd);
		 */
		table1.setHeaderVisible(false);
		table1.setLinesVisible(false);

		table1.addListener(SWT.MeasureItem, new Listener() {
			public void handleEvent(Event event) {
				event.width = table1.getGridLineWidth(); // 设置宽度

				event.height = (int) Math.floor(event.gc.getFontMetrics()
						.getHeight() * 1.5); // 设置高度为字体高度的3.5倍
				table1.setForeground(Display.getCurrent().getSystemColor(
						SWT.COLOR_WHITE));
			}
		});

		// table.addListener(SWT.EraseItem, new
		// SetTableColColorListener(table));
		final TableColumn acceleratorId = new TableColumn(table1, SWT.NONE);
		acceleratorId.setWidth(50);
		final TableColumn acceleratorName = new TableColumn(table1, SWT.CENTER);
		acceleratorName.setWidth(200);

		tableViewer1.setContentProvider(new AcceleratorContentProvider());
		tableViewer1.setLabelProvider(new AcceleratorLableProvider());
		tableViewer1.setInput(getAccelerator());

	}

	private List getAccelerator() {
		// TODO Auto-generated method stub
		List list = new ArrayList();
		list = (List) DbManager.getInstance().search(
				"com.whlilong.jxc.ui.InfoDialog.getAccelerator");
		if (list.toArray().length == 0)
			list.add(new Object[0]);
		return list;
	}

	private void createTableView(Composite parent) {
		// TODO Auto-generated method stub
		// TODO Auto-generated method stub

		tableViewer = new TableViewer(parent, SWT.BORDER | SWT.FULL_SELECTION);
		tableViewer.setUseHashlookup(true);
		table = tableViewer.getTable();
		table.setBackground(parent.getBackground());
		FormData fd = new FormData();
		fd.height = 400;
		fd.top = new FormAttachment(title2, 10);
		fd.left = new FormAttachment(2, 0);
		fd.right = new FormAttachment(80, 0);
		table.setLayoutData(fd);

		table.setHeaderVisible(true);
		table.setLinesVisible(false);

		table.addListener(SWT.MeasureItem, new Listener() {
			public void handleEvent(Event event) {
				event.width = table.getGridLineWidth(); // 设置宽度

				event.height = (int) Math.floor(event.gc.getFontMetrics()
						.getHeight() * 2); // 设置高度为字体高度的3.5倍
				table.setForeground(Display.getCurrent().getSystemColor(
						SWT.COLOR_WHITE));
			}

		});
		Font font = new Font(Display.getCurrent(), "宋体", 15, SWT.NONE);
		table.setFont(font);
		// table.addListener(SWT.EraseItem, new
		// SetTableColColorListener(table));
		final TableColumn goodsid = new TableColumn(table, SWT.NONE);
		goodsid.setText("编号");
		goodsid.setWidth(100);
		goodsid.setResizable(false);		
		final TableColumn goodsName = new TableColumn(table, SWT.CENTER);
		goodsName.setText("商品名称");
		goodsName.setWidth(200);
		goodsName.setResizable(false);
		final TableColumn goodsUnit = new TableColumn(table, SWT.CENTER);
		goodsUnit.setText("单位");
		goodsUnit.setWidth(100);
		goodsUnit.setResizable(false);
		final TableColumn goodsNum = new TableColumn(table, SWT.CENTER);
		goodsNum.setText("数量");
		goodsNum.setWidth(100);
		goodsNum.setResizable(false);
		final TableColumn goodsZk = new TableColumn(table, SWT.CENTER);
		goodsZk.setText("折扣/特价");
		goodsZk.setWidth(150);
		goodsZk.setResizable(false);
		final TableColumn retailprice = new TableColumn(table, SWT.CENTER);
		retailprice.setText("零售价");
		retailprice.setWidth(150);
		retailprice.setResizable(false);
		final TableColumn goodsSum = new TableColumn(table, SWT.CENTER);
		goodsSum.setText("小计");
		goodsSum.setWidth(200);
		goodsSum.setResizable(false);
		tableViewer.setContentProvider(new GoodsInfoContentProvider());
		tableViewer.setLabelProvider(new ClientGoodsInfoLableProvider());

		table.select(0);
		table.setFocus();
	}

	private List getSysfun() {
		// TODO Auto-generated method stub
		List list = new ArrayList();
		list = (List) DbManager.getInstance().search(
				"com.whlilong.jxc.ui.SysFunction.getSysfun");
		if (list.toArray().length == 0)
			list.add(new Object[0]);

		return list;
	}

	public void sale() {
		MessageDialog.openInformation(null, "uu", "uuu");
	}

	@Override
	public void setFocus() {
		// TODO Auto-generated method stub

	}

}
